#include "StartScene.h"
#include "D3DInput.h"

StartScene::StartScene(IDirect3DTexture9* background){
	mBackTex = background;
	loadTexture();
	btStory = new Button(mStory, mStoryBack, true);
	btLoadMap = new Button(mLoadMap, mLoadMapBack, false);
	btHighScore = new Button(mHighScore, mHighScoreBack, false);
	btExitGame = new Button(mExitGame, mExitGameBack, false);

	btStory->getPos().x = 0.0f;
	btStory->getPos().y = 0.0f;

	btLoadMap->getPos().x = 0.0f;
	btLoadMap->getPos().y = -20.0f;

	btHighScore->getPos().x = 0.0f;
	btHighScore->getPos().y = -40.0f;

	btExitGame->getPos().x = 0.0f;
	btExitGame->getPos().y = -60.0f;

	mBackgroundPos = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	mBackgroundCenter = D3DXVECTOR3((float)1920/2, (float)1080/2, 0.0f);

	count = 0.0f;
}

StartScene::~StartScene(){
	mBackTex = NULL;
	delete btStory;
	delete btLoadMap;
	delete btHighScore;
	delete btExitGame;
	releaseTexture();
}

SceneRS StartScene::update(float dt){
	//pos
	SceneRS rs;
	rs.ID = -1;
	rs.Info = "";
	D3DDEVICE_CREATION_PARAMETERS cparams;
	RECT rect;

	gDevice->GetCreationParameters(&cparams);
	GetWindowRect(cparams.hFocusWindow, &rect);

	mWidth = rect.right - rect.left;
	mHeight= rect.bottom - rect.top;

	if(count>0.0f){
		count-=dt;

		if(count<= 0.0f){
			count =0.0f;
		}
		return rs;
	}

	//key event
	if(gInput->keyDown(DIK_DOWNARROW)){
		if(btStory->getSelected()){
			btStory->setSelected(false);
			btLoadMap->setSelected(true);
		}else if(btLoadMap->getSelected()){
			btLoadMap->setSelected(false);
			btHighScore->setSelected(true);
		}else if(btHighScore->getSelected()){
			btHighScore->setSelected(false);
			btExitGame->setSelected(true);
		}else if(btExitGame->getSelected()){
			btExitGame->setSelected(false);
			btStory->setSelected(true);		
		}
		count = 0.2f;
		return rs;
	}

	if(gInput->keyDown(DIK_UPARROW)){
		if(btExitGame->getSelected()){
			btExitGame->setSelected(false);
			btHighScore->setSelected(true);
		}else if(btHighScore->getSelected()){
			btHighScore->setSelected(false);
			btLoadMap->setSelected(true);
		}else if(btLoadMap->getSelected()){
			btLoadMap->setSelected(false);
			btStory->setSelected(true);
		}else{
			btStory->setSelected(false);
			btExitGame->setSelected(true);
		}
		count = 0.2f;
		return rs;
	}

	if(gInput->keyDown(DIK_RETURN)){
		if(btStory->getSelected()){
			rs.ID = 0; //
			count = 0.2f;
		}else if(btLoadMap->getSelected()){
			rs.ID = 1; //
			count = 0.2f;
		}else if(btHighScore->getSelected()){
			rs.ID = 2; //
			count = 0.2f;
		}else {
			exit(1);
		}
	}

	return rs;
}

void StartScene::draw(ID3DXSprite* sprite){
	drawBackground(sprite);
	btStory->render(sprite);
	btLoadMap->render(sprite);
	btHighScore->render(sprite);
	btExitGame->render(sprite);
}

void StartScene::drawBackground(ID3DXSprite* sprite){
	if(mBackTex == 0) return;

	float rate  = 1.0f;
	float wrate = (float)mWidth/1920;
	float hrate = (float)mHeight/1080;
	if(wrate > hrate){
		rate = wrate;
	}else{
		rate = hrate;
	}
	rate = ceilf(rate * 100) / 100; 
	rate = rate/2;
	D3DXMATRIX S;
	D3DXMatrixScaling(&S,rate,-rate,1.0f);
	sprite->SetTransform(&(S));
	sprite->Draw(mBackTex,0,&mBackgroundCenter,0,D3DCOLOR_XRGB(255, 255, 255));
	sprite->Flush();
}

void StartScene::loadTexture(){

	mStory = 0;
	D3DXCreateTextureFromFileEx(gDevice,"Sprites/Button/Story_High.png",
		D3DX_DEFAULT_NONPOW2,
		D3DX_DEFAULT_NONPOW2,
		0,
		0,
		D3DFMT_UNKNOWN,
		D3DPOOL_MANAGED,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(0,0,0),
		NULL,
		NULL,
		&mStory);

	mStoryBack = 0;
	D3DXCreateTextureFromFileEx(gDevice,"Sprites/Button/Story_Back.png",
		D3DX_DEFAULT_NONPOW2,
		D3DX_DEFAULT_NONPOW2,
		0,
		0,
		D3DFMT_UNKNOWN,
		D3DPOOL_MANAGED,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(0,0,0),
		NULL,
		NULL,
		&mStoryBack);

	mLoadMap = 0;
	D3DXCreateTextureFromFileEx(gDevice,"Sprites/Button/LoadMap_High.png",
		D3DX_DEFAULT_NONPOW2,
		D3DX_DEFAULT_NONPOW2,
		0,
		0,
		D3DFMT_UNKNOWN,
		D3DPOOL_MANAGED,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(0,0,0),
		NULL,
		NULL,
		&mLoadMap);

	mLoadMapBack = 0;
	D3DXCreateTextureFromFileEx(gDevice,"Sprites/Button/LoadMap_Back.png",
		D3DX_DEFAULT_NONPOW2,
		D3DX_DEFAULT_NONPOW2,
		0,
		0,
		D3DFMT_UNKNOWN,
		D3DPOOL_MANAGED,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(0,0,0),
		NULL,
		NULL,
		&mLoadMapBack);

	mHighScore = 0;
	D3DXCreateTextureFromFileEx(gDevice,"Sprites/Button/HighScore_High.png",
		D3DX_DEFAULT_NONPOW2,
		D3DX_DEFAULT_NONPOW2,
		0,
		0,
		D3DFMT_UNKNOWN,
		D3DPOOL_MANAGED,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(0,0,0),
		NULL,
		NULL,
		&mHighScore);

	mHighScoreBack = 0;
	D3DXCreateTextureFromFileEx(gDevice,"Sprites/Button/HighScore_Back.png",
		D3DX_DEFAULT_NONPOW2,
		D3DX_DEFAULT_NONPOW2,
		0,
		0,
		D3DFMT_UNKNOWN,
		D3DPOOL_MANAGED,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(0,0,0),
		NULL,
		NULL,
		&mHighScoreBack);

	mExitGame = 0;
	D3DXCreateTextureFromFileEx(gDevice,"Sprites/Button/ExitGame_High.png",
		D3DX_DEFAULT_NONPOW2,
		D3DX_DEFAULT_NONPOW2,
		0,
		0,
		D3DFMT_UNKNOWN,
		D3DPOOL_MANAGED,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(0,0,0),
		NULL,
		NULL,
		&mExitGame);

	mExitGameBack = 0;
	D3DXCreateTextureFromFileEx(gDevice,"Sprites/Button/ExitGame_Back.png",
		D3DX_DEFAULT_NONPOW2,
		D3DX_DEFAULT_NONPOW2,
		0,
		0,
		D3DFMT_UNKNOWN,
		D3DPOOL_MANAGED,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(0,0,0),
		NULL,
		NULL,
		&mExitGameBack);
}

void StartScene::releaseTexture(){

	if(mStory != 0){
		ReleaseCOM(mStory);
	}

	if(mStoryBack != 0){
		ReleaseCOM(mStoryBack);
	}

	if(mLoadMap != 0){
		ReleaseCOM(mLoadMap);
	}

	if(mLoadMapBack != 0){
		ReleaseCOM(mLoadMapBack);
	}

	if(mHighScore != 0){
		ReleaseCOM(mHighScore);
	}

	if(mHighScoreBack != 0){
		ReleaseCOM(mHighScoreBack);
	}

	if(mExitGame != 0){
		ReleaseCOM(mExitGame);
	}

	if(mExitGameBack != 0){
		ReleaseCOM(mExitGameBack);
	}
}